#include "AboutScene.h"
#include "GraphCache.h"
#include "T_Text.h"
#include "T_Engine.h"
#include "T_Sprite.h"
#include "MoveTo.h"

class AboutListener:public T_Ref{
public:
	static AboutListener *create(){
		AboutListener *ret=new AboutListener();
		return ret;
	}

	void onMouse(T_Node *node,int y,int x,MouseEvent e){
		if(node==NULL)return;
		T_Sprite *sprite=dynamic_cast<T_Sprite *>(node);
		if(sprite==NULL){return;}	
		GraphCache &cache=*GraphCache::getInstance();
		switch(e){
		case MouseIn:
			sprite->setTexture(cache.get("back")->getRegion(1),false);
			return;
		case MouseOut:
			sprite->setTexture(cache.get("back")->getRegion(0),false);
			return;
		}
	}

	void onTouch(T_Node *node,int y,int x,TouchEvent e){
		if(NULL==node || e!=TouchEnd){
			return;
		}
		T_Engine::pEngine->resumeScene();
	}
};

AboutScene::~AboutScene(){
	if(m_Listener!=NULL){
		m_Listener->release();
		m_Listener=NULL;
	}
}

AboutScene*AboutScene::create(){
	AboutScene *ret=new AboutScene();
	ret->onInit();
	return ret;
}

void AboutScene::onInit(){
	m_Listener=AboutListener::create();
	GraphCache &cache=*GraphCache::getInstance();
	T_Sprite *BackButton=T_Sprite::create(cache.get("back")->getRegion(0));

	T_Sprite *background=T_Sprite::create(cache.get("background_1")->getBMP());
//	background->setScale(1.2f);

	SizeF wndSize=T_Engine::pEngine->GetSize();

	background->setPosition(PointF((wndSize.Width-background->getWorldBounds().Width)/2,
		(wndSize.Height-background->getWorldBounds().Height)/2));
	background->setLocalZOrder(0);

	addChild(background);
//	BackButton->setLocalZOrder();
	BackButton->setTouchEnabled(true);
	BackButton->addMouseEventListener(m_Listener,(MouseEventCallee)&AboutListener::onMouse);
	BackButton->addTouchEventListener(m_Listener,(TouchEventCallee)&AboutListener::onTouch);
	addChild(BackButton);

	PointF point;
	point.X=wndSize.Width/4;
	point.Y=wndSize.Height/3;
	for(int i=0;i<3;i++){
		T_BITMAP *img=cache.get(2015+i);
		T_Sprite *sp=T_Sprite::create(img->getBMP());
		sp->runAction(MoveTo::create(point,5));
		sp->setLocalZOrder(i+1);
		point.X+=200;
		point.Y+=(rand()%100-50);
		addChild(sp);
	}
}

void AboutScene::onShow(){
}

void AboutScene::onPause(){
	m_Listener->release();
//	release();
}